package com.zang.blogz.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zang.blogz.constant.RedisConstant;
import com.zang.blogz.dto.SocialTokenDTO;
import com.zang.blogz.entity.UserInfo;
import com.zang.blogz.entity.UserRole;
import com.zang.blogz.mapper.UserInfoMapper;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.RedisService;
import com.zang.blogz.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zang.blogz.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RedisService redisService;

    @Override
    public Result<String> banned(Integer userId, Integer day) {

        /*
        * 因为封号之后不能对当前已经登录的用户立刻禁封，所以我们先将其踢下线再禁封
        * */

        StpUtil.kickout(userId);
        StpUtil.disable(userId, 86400L * day);

        return Result.ok("已对其封号");
    }

    @Override
    @Transactional
    public Result<List<UserRole>> updateRole(List<Integer> roles) {

        Integer loginId = (Integer) StpUtil.getLoginId();

        List<UserRole> userRoles = userRoleService.updateResource(loginId, roles);

        deleteSecurityCache();

        return Result.ok(userRoles);

    }

    @Transactional
    public void deleteSecurityCache() {

        redisService.delVague(RedisConstant.REG_USER_MENU);
        redisService.delVague(RedisConstant.REG_USER_ROLE);
        redisService.delVague(RedisConstant.REG_USER_RESOURCE);

    }



}
